package com.robot.admin.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.robot.admin.dao.CityDao;
import com.robot.admin.pojo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;


/**
 * city服务层
 *
 * @author lin
 */
@Service
public class CityService {
    @Autowired
    private CityDao cityDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<City> findAll() {
        return cityDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<City> findSearch(Map whereMap, int page, int size) {
        Specification<City> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return cityDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<City> findSearch(Map whereMap) {
        Specification<City> specification = createSpecification(whereMap);
        return cityDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param code
     * @return
     */
    public City findById(String code) {
        return cityDao.findById(code).get();
    }

    /**
     * 增加
     *
     * @param city
     */
    public void add(City city) {
        //雪花分布式ID生成器
        city.setCode(idWorker.nextId() + "");
        cityDao.save(city);
    }

    /**
     * 修改
     *
     * @param city
     */
    public void update(City city) {
        cityDao.save(city);
    }

    /**
     * 删除
     *
     * @param code
     */
    public void deleteById(String code) {
        cityDao.deleteById(code);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<City> createSpecification(Map searchMap) {
        return new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编码
                String code = "code";
                if (searchMap.get(code) != null && !"".equals(searchMap.get(code))) {
                    predicateList.add(cb.like(root.get(code).as(String.class), "%" + (String) searchMap.get(code) + "%"));
                }
                // 市名
                String name = "name";
                if (searchMap.get(name) != null && !"".equals(searchMap.get(name))) {
                    predicateList.add(cb.like(root.get(name).as(String.class), "%" + (String) searchMap.get(name) + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 根据code查找city列表
     *
     * @param code
     * @return
     */
    public Page<City> findAllByCodeLike(String code, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return cityDao.findAllByCodeLike(code, pageable);
    }

    /**
     * 根据code查找city
     *
     * @param code
     * @return
     */
    public City findByCode(String code) {
        return cityDao.findByCode(code);
    }

    /**
     * 根据name查找city
     *
     * @param name
     * @return
     */
    public City findByName(String name) {
        return cityDao.findByName(name);
    }
}
